package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.domain.entity.User;
import com.ruoyi.mapper.UserMapper;
import com.ruoyi.service.UserService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUserByPhone(String phone) {
        return userMapper.selectUserByPhone(phone);
    }

    @Override
    public boolean updatePassword(String phone, String password) {
        return userMapper.updatePasswordByPhone(phone,password);
    }

    @Override
    public boolean checkPassword(String phone, String password) {
        String userPassword = getPasswordByPhone(phone);
        return BCrypt.checkpw(password,userPassword);
    }

    @Override
    public String getPasswordByPhone(String phone) {
        if (ObjectUtils.isEmpty(phone)) {
            return null;
        }
        return userMapper.selectPasswordByPhone(phone);
    }

    @Override
    public User getUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    @Override
    public void updatePhoneByOldPhone(String oldPhone, String newPhone) {
        userMapper.updatePhoneByOldPhone(oldPhone,newPhone);
    }

    @Override
    public void logoutUser(String phone) {
        userMapper.updateUserStatus(phone,1);
    }

    @Override
    public boolean updateUserInfo(User user) {
        return userMapper.updateUserInfo(user);
    }
}